
import { FairyGUI } from "csharp";
import LayerType from "./LayerType";
import UIBaseWindow from "./UIBaseWindow";

export default class UISystem {


    private static _Inst: UISystem;
    public static get Inst(): UISystem {
        if (this._Inst == null) this._Inst = new UISystem();
        return this._Inst;
    }

    //显示对象缓存
    private m_view: Object = new Object();
    //等待回收显示对象缓存
    private m_gc_view: Object = new Object();

    public HasView(cls: any): boolean  {

        return this.m_view[cls] != null;
    }

    /**
     * 在主界面层以上是否有任何界面
     */
    public hasAnyWindow():boolean{

        let window:any = null;
        let key:any;
        for( key in this.m_view ){
            window = this.m_view[ key ];
            // if( window instanceof UIMainView || window instanceof UIStartView || window.parent == GameLayer.fightLayer ){
            //     continue;
            // }
            // if( window instanceof UITipView ){
            //     continue;
            // }
            if( window instanceof UIBaseWindow ){
                return true;
            }
        }
        return false;
    }

    /**
	 * 统一创建界面窗口类接口，不要其它地方创建
	 * @param cls	创建类名
	 */
    public CreateWindowView(cls: any, param: any = null): any {

        if (cls == null) {
            return;
        }
        let view: UIBaseWindow = this.m_view[cls];

        if (view != null && view.parent != null ){
            return;
        } 

        if (view == null) {
            if (view == null) {
                view = new cls();
                view.cls = cls;
            }
            this.m_view[cls] = view;
        }

        LayerType.SetWindowLayer(view);
        view.addAllListener();
        view.show(param);
        return view;
    }

	/**
	 * 移除传入的窗口界面类
	 * @param cls 界面类
	 */
    public RemoveWindowClass(cls: any): boolean {

        return this.RemoveWindowView(this.m_view[cls]);
    }

    /**
     * 移除对应界面层所有界面
     * @param layer 
     */
    public removeAllFrom( layers:Array<number> = null ):void{

        let window:any = null;
        let key:any;
        for( key in this.m_view ){
            window = this.m_view[ key ];
            if( window instanceof UIBaseWindow ){
                if( layers == null || layers.indexOf( window.GetViewStruct().layerType ) != -1 ){
                    this.RemoveWindowView( window );
                }
            }
        }
    }

	/**
	 * 移除传入的窗口界面对象,并从缓存中清除
	 */
    public RemoveWindowView(view: UIBaseWindow): boolean {

        if (view == null || view.parent == null) {
            return false;
        }

        let _resName:string = view.panelName;
        let _self: UISystem = this;

        view.RemoveFromParent();
        view.removeListener();
        view.Reset();
        if (view.cls in this.m_view) {
            delete this.m_view[view.cls];
            this.m_gc_view[view.cls] = view;
        }

        // GameDispatcher.Inst.event( UIGameEvent.HIDE_VIEW , _resName );

        return true;
    }

	/**
	 * 获取窗口
	 */
    public GetWindowView(cls: any): any {

        return this.m_view[cls];
    }

    /**
     * 获取对应的界面
     * @param panelName 面板名字
     */
    public getWindowViewBy( panelName:string ):any{

        let key:any;
        for( key in this.m_view ){
            let view:UIBaseWindow = this.m_view[ key ];
            if( view && view.panelName == panelName ){
                return view;
            }
        }
        return null;
    }

    /**
     * 获取顶层的
     */
    public getTopWindow():UIBaseWindow{

        let key:any;
        let prev = -1;
        let view:UIBaseWindow = null;
        for( key in this.m_view ){
            let v = this.m_view[ key ];
            if( v instanceof UIBaseWindow && v.parent ){
                let index = FairyGUI.GRoot.inst.GetChildIndex( v.parent );
                if( index > prev ){
                    view = v;
                    prev = index;
                }
            }
        }
        return view;
    }

    /**
     * 元件是否在对应界面中
     * @param child 
     * @param panelName 面板名字活着面板类
     */
    public getChildInView( child:FairyGUI.GObject , panelName:any = null ):boolean{

        if( child ){
            if( child.parent && (child.parent.name == panelName || child.parent == panelName ) ){// || child.parent instanceof panelName 
                return true;
            }else{
                return this.getChildInView( child.parent , panelName );
            }
        }
        return false;
    }
}